﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows;
using FinPlusCommon;
using u = FinPlusUtility.Utility;

namespace FinPlusWPFControls
{
    public static class ChartFormaters
    {
        public static string GetTitle(IFinPlusFunction func, string[] titles, TimeSpan interval, DateTime start, DateTime end, string dateFormat)
        {
            var res = string.Empty; ;
            foreach (var title in titles)
                res += string.Format(" {1}", title, func.Get(title).Value);
            if(!interval.Equals(TimeSpan.MinValue))
                res += string.Format(" Interval({0})", interval.ToString());
 
            res += string.Format(" Period({0}, {1})", start.ToString(dateFormat), end.ToString(dateFormat));

            return res;
        }

        public static Style GetDashedLineStyle()
        {
            var style = new Style(typeof(Polyline));
            style.Setters.Add(new Setter(Shape.StrokeDashArrayProperty,  new DoubleCollection(new[] { 5.0 })));
            return style;
        }
    }

    public class ChartDataPoint
    {
        public DateTime DateTime { get; set; }
        public double High { get; set; }
        public double Mid { get { return (High + Low) / 2; } }
        public double Low { get; set; }
        public double Open { get; set; }
        public double Close { get; set; }
        public double Volume { get; set; }
        public string Name { get; set; }

        public double Get(string field)
        {
            switch (field)
            {
                case "High": return High;
                case "Mid": return Mid;
                case "Low": return Low;
                case "Open": return Open;
                case "Close": return Close;
                case "Volume": return Volume;
                default: return double.NaN;
            }
        }

        public void Add(IFinPlusItem item, string type)
        {
            Name = item.Name;

            switch (type)
            {
                case "HIGH": High = double.Parse(item.Value); break;
                case "LOW": Low = double.Parse(item.Value); break;
                case "OPEN": Open = double.Parse(item.Value); break;
                case "CLOSE": Close = double.Parse(item.Value); break;
                case "VOLUME": Volume = double.Parse(item.Value); break;
                case "DATETIME": DateTime = DateTime.Parse(item.Value); break;
            }
        }
    }
}
